import { BasicColumn, FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import { DescItem } from '/@/components/Description/index';
import { useI18n } from '/@/hooks/web/useI18n';
import { readonly } from 'vue';
import { filter } from 'lodash';
import DictEnum from '/@/enums/dictEnum';
const { t } = useI18n('');

export enum Status {
   /**
     * 待审批
     */
   WAIT_APPROVE = "1",
   /**
    * 已审批
    */
   APPROVED = "1",
   /**
    * 拒绝
    */
   REFUSE = "3",
   /**
    * 撤回
    */
   UNDO = "4"
}


export enum SubjectType {
  /**
    * 角色
    */
  ROLE = "1",
  /**
   * 用户
   */
  USER = "2",
}

const getStatusColor = (value) => {
  let color = "warning"
  if (value == "1") {
    color = "warning"
  } else if (value == "2") {
    color = "green"
  } else if (value == "3") {
    color = "red"
  } else if (value == '4') {
    color = 'purple'
  }
  return color
}
export const columns: BasicColumn[] = [
  {
    title: t('申请资源'),
    dataIndex: 'resourceIds',
    slot: 'applyResource',
    width: 200,
  },
  {
    title: t('资源类型'),
    dataIndex: 'resourceTypeName',
    width: 100,
  },
  {
    title: t('申请主体类型'),
    dataIndex: 'subjectTypeName',
    width: 100,
  },
  {
    title: t('申请主体ID'),
    dataIndex: 'subjectId',
    width: 130,
  },
  {
    title: t('申请时间'),
    dataIndex: 'applyTime',
    width: 160,
  },
  {
    title: t('申请人'),
    dataIndex: 'applyUserName',
    width: 120,
  },
  {
    title: t('审批人'),
    dataIndex: 'approvalUserName',
    width: 120,
    customRender: ({ text }) => {
      return text ? text : '-'
    },
  },
  {
    title: t('审批时间'),
    dataIndex: 'approvalTime',
    width: 160,
    customRender: ({ text }) => {
      return text ? text : '-'
    },
  },
  {
    title: t('审批状态'),
    dataIndex: 'statusName',
    customRender: ({ text, record }) => {
      
      return render.renderTag(text, getStatusColor(record?.status))
    },
    width: 100,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: t('申请时间'),
    field: 'applyTime',
    component: 'RangePicker',
    componentProps: {
      valueType: "Date",
    },
  },
  {
    label: t('申请用户ID'),
    field: 'applyUserId',
    component: 'Input',
  },
  {
    label: t('申请用户名'),
    field: 'applyUserName',
    component: 'Input',
  },
];

/**
 * 审批表单
 */
export const approvalInputFormSchemas: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: t('申请资源'),
    required: true,
    field: 'resourceIds',
    component: "MyTreeSelect",
    componentProps: {
      disabled: true,
      allowClear: true,
      multiple: true,
      fieldNames: {
        title: "name",
        key: "id",
        value: "id",
      },
    }
  },
  {
    // 目前就支持菜单
    label: t('资源类型'),
    required: true,
    field: 'resourceTypeName',
    component: "Input",
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    defaultValue: '未知',
    colProps: {
      span: 12
    }
  },
  {
    label: t('申请主体ID'),
    required: false,
    field: 'subjectId',
    component: 'Input',
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    colProps: {
      span: 12
    }
  },
  {
    label: t('主体类型'),
    required: true,
    field: 'subjectTypeName',
    component: "Input",
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    defaultValue: '未知',
    colProps: {
      span: 12
    }
  },
  {
    label: t('主体名称'),
    required: true,
    field: 'subjectName',
    component: "Input",
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    defaultValue: '未知',
    colProps: {
      span: 12
    }
  },
  {
    label: t('申请人'),
    field: 'applyUserName',
    component: 'Input',
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    colProps: {
      span: 12
    }
  },
  {
    label: t('申请企业'),
    field: 'tenantName',
    component: 'Input',
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    colProps: {
      span: 12
    }
  },
  {
    label: t('申请原因'),
    required: true,
    field: 'applyReason',
    component: "Input",
    render: ({ model, field }) => {
      return render.renderSpan(model[field])
    },
    colProps: {
      span: 24
    }
  },
  {
    label: t('审批状态'),
    required: true,
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      filter: (v) => v != Status.UNDO, 
      type: "radio",
      dictType: 'permApprovalStatus',
    },
  },
  {
    label: t('拒绝原因'),
    required: true,
    field: 'refuseReason',
    component: "InputTextArea",
    ifShow: ({ model, field }) => model['status'] == Status.REFUSE,
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },
];

// 权限申请表单
export const applyInputFormSchemas: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    defaultValue: null,
    show: false,
  },
  {
    // 目前就支持菜单
    label: t('资源类型'),
    required: true,
    field: 'resourceType',
    component: "JDictSelectTag",
    defaultValue: '1', 
    componentProps: {
      type: "radio",
      dictType: DictEnum.IamResourceType,
      filter: (item) => {
        return item === "1"
      },
    },
  },
  {
    label: t('主体类型'),
    required: true,
    field: 'subjectType',
    component: "JDictSelectTag",
    defaultValue: '2', 
    componentProps: {
      type: "radio",
      dictType: 'ramSubjectType',
    },
  },
  {
    label: t('所选主体'),
    required: true,
    field: 'subjectId',
    component: "ApiSelect",
    ifShow: ({ model, field }) => model["subjectType"] == SubjectType.ROLE,
  },
  // 展示名字
  {
    label: t('所选主体'),
    required: true,
    field: 'userSubjectName',
    component: "Input",
    ifShow: ({ model, field }) => model["subjectType"] == SubjectType.USER,
    componentProps: {
      readonly: true,
    },
  },
  {
    field: 'resourceIds',
    label: t('申请资源'),
    component: "MyTreeSelect",
    required: true,
    componentProps: {
      allowClear: true,
      multiple: true,
      fieldNames: {
        title: "name",
        key: "id",
        value: "id",
      },
    }
  },

  {
    label: t('申请人'),
    field: 'applyUserName',
    component: 'Input',
    componentProps: {
      readonly: true,
    },
  },
  {
    label: t('申请企业'),
    field: 'applyTenantName',
    component: 'Input',
    componentProps: {
      readonly: true,
    },
  },
  {
    label: t('申请原因'),
    required: true,
    field: 'applyReason',
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },
];



export const formDetailSchema: DescItem[] = [
  {
    field: "divider-1",
    label: "",
    render: (curVal, data) => {
      return render.renderDividerWithValue('申请信息')
    },
    span: 2,
  },
  {
    label: t('申请资源'),
    field: 'resourceNames',
    span: 2
  },
  {
    label: t('资源类型'),
    field: 'resourceTypeName'
  },
  {
    label: t('申请主体类型'),
    field: 'subjectTypeName'
  },
  {
    label: t('申请主体ID'),
    field: 'subjectId'
  },
  {
    label: t('申请主体名称'),
    field: 'subjectName'
  },
  {
    label: t('申请原因'),
    field: 'applyReason'
  },
  {
    label: t('申请时间'),
    field: 'applyTime'
  },
  {
    label: t('申请人ID'),
    field: 'applyUserId'
  },
  {
    label: t('申请人用户名'),
    field: 'applyUserName'
  },
  {
    label: t('申请租户'),
    field: 'tenantName',
    span: 2,
  },
  {
    field: "divider-1",
    label: "",
    render: (curVal, data) => {
      return render.renderDividerWithValue('审批信息')
    },
    span: 2,
  },
  {
    label: t('审批状态'),
    field: 'statusName',
    render: (curVal, data) => {
      return render.renderTag(curVal, getStatusColor(data.status))
    },
  },
  {
    label: t('审批租户'),
    field: 'approvalTenantName'
  },
  {
    label: t('审批人ID'),
    field: 'approvalUserId'
  },
  {
    label: t('审批人用户名'),
    field: 'approvalUserName'
  },
  {
    label: t('审批时间'),
    field: 'approvalTime',
    span: 2,
  },
  {
    label: t('拒绝原因'),
    field: 'refuseReason',
    span: 2
  },
];
